//
//  YCBlurryImageViewController.m
//  DemoBlurryImage
//
//  Created by 唐可雄 on 16/9/2.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCBlurryImageViewController.h"

#import "UIImage+CoreImage.h"
#import "UIImage+ImageEffects.h"

@interface YCBlurryImageViewController () {
    UIVisualEffectView *_effectView;
}

@end

@implementation YCBlurryImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self typeDidChange:self.typeSegmentCtrl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)typeDidChange:(id)sender {
    if (self.typeSegmentCtrl.selectedSegmentIndex == 0) {
        [_effectView removeFromSuperview];
        
        __weak typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            UIImage *img = [UIImage blurImageOf:weakSelf.srcImgView.image];
            dispatch_async(dispatch_get_main_queue(), ^{
                __strong typeof(weakSelf) strongSelf = weakSelf;
                strongSelf.blurImgView.image = img;
            });
        });
    } else if (self.typeSegmentCtrl.selectedSegmentIndex == 1) {
        [_effectView removeFromSuperview];
        
        self.blurImgView.image = [self.srcImgView.image blurImage];
    } else {
        if (_effectView == nil) {
            _effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
            _effectView.frame = self.srcImgView.frame;
            
//            UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)_effectView.effect]];
//            subEffectView.frame = _effectView.frame;
//            [_effectView.contentView addSubview:subEffectView];
        }
        
        [self.view addSubview:_effectView];
    }
}

@end
